package com.i2863.receipt.userUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.i2863.receipt.util.HttpSessionUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;

/**
 * 用户中心
 * @author  xuweiwei
 * @date  2017/6/20
 */
public class UserCenter {

    private static CloseableHttpClient client = HttpSessionUtils.buildHttpClient();
    private static Logger logger = LoggerFactory.getLogger(UserCenter.class);


    /**
     * 调用用户中心接口,获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     * @throws IOException
     */
    public static User getUserByName(String username) {
        Map<String, String> param = new HashMap<String, String>(20);
        param.put("method", "getUserInfo");
        param.put("username", username);
        param.put("siteId", "136");
        String jsonStr = null;
        try {
            jsonStr = HttpSessionUtils.httpPost(client, Constant.ucenter, param);
        } catch (IOException e) {
            throw new UserException(e);
        }
        JSONObject json = JSON.parseObject(jsonStr);
        System.out.println(json.toJSONString());
        int code = json.getInteger("code");
        if (code != 0) {
            throw new RuntimeException("illegal code:" + code);
        }
        JSONObject data = json.getJSONObject("data");
        String logo = data.getString("logo");
        String nickName = data.getString("nickName");
        Integer siteId = Integer.parseInt(data.getString("siteId"));
        return new User(username, nickName, logo,siteId);
    }


    public static void loadUserInfo(Map<?, User> map) {
        loadUserInfo(map.values());
    }

    /**
     * 获取所有用户信息
     *
     * @param userList 用户列表
     * @throws IOException
     */
    public static void loadUserInfo(Collection<User> userList) {
        if(userList==null || userList.size()==0){
            return;
        }
        //拼接用户名
        StringBuilder users = new StringBuilder(userList.size() * 12);
        Set<String> userSet = new HashSet<String>();
        for (User u : userList) {
            if (userSet.contains(u.getUsername())) {
                continue;
            }
            if (users.length() > 0) {
                users.append(",");
            }
            users.append(u.getUsername());
        }
        //调用用户中心接口
        Map<String, String> param = new HashMap<String, String>(20);
        param.put("method", "getMultiUserInfo");
        param.put("usernames", users.toString());
        String jsonStr = null;
        try {
            jsonStr = HttpSessionUtils.httpPost(client, Constant.ucenter, param);
        } catch (IOException e) {
            throw new UserException(e);
        }
        JSONObject json = JSON.parseObject(jsonStr);
        int code = json.getInteger("code");
        if (code != 0) {
            throw new RuntimeException("illegal code:" + code);
        }
        JSONArray data = json.getJSONArray("data");
        Map<String, User> map = new HashMap<String, User>(20);
        for (Object obj : data) {
            JSONObject jsonObject = (JSONObject) obj;
            String username = jsonObject.getString("username");
            String nickName = jsonObject.getString("nickName");
            String logo = jsonObject.getString("logo");
            Integer siteId = jsonObject.getInteger("siteId");

            User u = new User(username, nickName, logo,siteId);
            map.put(username, u);
        }
        //装载用户信息
        for (User u : userList) {
            User u1 = map.get(u.getUsername());
            //查不到此用户的信息
            if (u1 == null) {
                String userName = u.getUsername();
                u.setNickName(userName.substring(0, 3) + "****" + userName.substring(7));
                u.setLogo("");
            } else {
                u.setNickName(u1.getNickName());
                u.setLogo(u1.getLogo());
                u.setSiteId(u1.getSiteId());
            }
        }
    }

    public static void main(String[] args) throws IOException {
        List<User> userList = new ArrayList<User>();
        userList.add(new User("13980974244"));
        userList.add(new User("17760513734"));
        loadUserInfo(userList);
        for (User u : userList) {
            System.out.println(JSONObject.toJSONString(u));
        }
//        System.out.println(JSONObject.toJSONString(getUserByName("13500849649")));
    }
}
